home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ARASAN_S.ZIP / SQUARE.H < prev    next >
C/C++ Source or Header  |  1994-02-19  |  2KB  |  129 lines

  1. // Copyright 1994 by Jon Dart.  All Rights Reserved.
  2.  
  3. #ifndef _SQUARE_H
  4. #define _SQUARE_H
  5.  
  6. #include <assert.h>
  7. #include "types.h"
  8. #include "color.h"
  9.  
  10. class Square;
  11.  
  12. class Square
  13. {
  14.     public:    
  15.     
  16.     friend Square operator +( const Square &sq, const int i);
  17.     friend Square operator -( const Square &sq, const int i);
  18.  
  19.     enum {InvalidSquare = 127};
  20.  
  21.     Square()
  22.     {
  23.        my_location = InvalidSquare;
  24.     }
  25.  
  26.     Square( const int location )
  27.     {
  28.        my_location = location;
  29.     }
  30.     
  31.     Square( const int file, const int rank, const ColorType side );
  32.     
  33.     operator int() const
  34.     {
  35.        return my_location;     
  36.     }
  37.     
  38.     Square &operator +=( const int i)
  39.     {
  40.         my_location += i;
  41.     return *this;
  42.     }
  43.     
  44.     Square &operator -=( const int i)
  45.     {
  46.         my_location -= i;
  47.     return *this;
  48.     }
  49.  
  50.     Square &operator ++()
  51.     {
  52.         my_location++;
  53.     return *this;
  54.     }
  55.  
  56.     Square &operator --()
  57.     {
  58.         my_location--;
  59.     return *this;
  60.     }
  61.  
  62.     int Rank( const ColorType side = White) const;
  63.     // returns "rank" from the perspective of "side".  1 = 1st
  64.     // rank for side, 8 = last rank.        
  65.  
  66.     int File() const
  67.     {
  68.        return Files[my_location];
  69.     }
  70.     // returns file.  Queen rook file = 1, King rook file = 8.
  71.         
  72.     Boolean OnBoard() const
  73.     {
  74.       return (my_location & ~0x3f) == 0;
  75.     }
  76.     
  77.     Boolean OnEdge() const
  78.     {
  79.       return Edge[my_location];        
  80.     }
  81.     
  82.     // invalid value, used as delimiter
  83.     static const Square &Invalid();
  84.  
  85.     Boolean IsInvalid() const
  86.     {
  87.        return my_location == InvalidSquare;        
  88.     }
  89.     
  90.     void SetInvalid()
  91.     {
  92.        my_location = InvalidSquare;
  93.     }
  94.     
  95.     ColorType Color() const
  96.     {
  97.        return (ColorType)Colors[my_location];
  98.     }
  99.     
  100.     static Square Value( char *p);
  101.     // Parse a square value in algebraic notation (e.g. "g7") and
  102.     // return a corresponding Square type.
  103.     
  104.     char FileImage() const;
  105.  
  106.     char RankImage() const;
  107.  
  108.     private:
  109.         
  110.     byte my_location;
  111.     static int Files[64];
  112.     static int Ranks[64];
  113.     static int Edge[64];
  114.     static int Colors[64];
  115. };
  116.  
  117. inline Square operator +( const Square &sq, const int i)
  118. {
  119.     return Square(sq.my_location + i);
  120. }
  121.  
  122. inline Square operator -( const Square &sq, const int i)
  123. {
  124.     return Square(sq.my_location-i);
  125. }
  126.  
  127. #endif
  128.  
  129.